/**
 * ©2013-2015 Alan L. Rights Reserved.
 */
package org.freedom.railhunter.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *
 * @author Alan
 */
public class Order implements Serializable {
    
    private LeftTicketItem leftTicketItem;
    
    private List<Passenger> passengers;
    
    private List<OrderTicketInfo> ticketsInfo;
    
    private SeatType seatType;
    
    private String purposeCodes;
    
    private Date trainDate;

    public Order(LeftTicketItem leftTicketItem, List<Passenger> passengers, SeatType seatType, 
            String purposeCodes, Date trainDate) {
        this.leftTicketItem = leftTicketItem;
        this.passengers = passengers;
        this.purposeCodes = purposeCodes;
        this.trainDate = trainDate;
        this.seatType = seatType;
        
        // 构建tickets info
        ticketsInfo = new ArrayList<>();
        for (Passenger passenger : passengers) {
            OrderTicketInfo ticketInfo = new OrderTicketInfo();
            ticketInfo.setOnlyId(OrderTicketInfo.generateTicketInfoOnlyId(ticketsInfo));
            ticketInfo.setSeatType(seatType.getValue());
            ticketInfo.setSeatTypeName(seatType.getName());
            ticketInfo.setPassengerIdType(passenger.getPassengerIdTypeCode());
            ticketInfo.setPassengerIdTypeName(passenger.getPassengerIdTypeName());
            ticketInfo.setPassengerIdNo(passenger.getPassengerIdNo());
            ticketInfo.setPassengerName(passenger.getPassengerName());
            ticketInfo.setPassengerPhoneNo(passenger.getMobileNo());
            ticketInfo.setTicketType(passenger.getPassengerType());
            ticketInfo.setTicketTypeName(OrderTicketInfo.getTicketTypeName(passenger.getPassengerType()));
            ticketsInfo.add(ticketInfo);
        }
    }

    public LeftTicketItem getLeftTicketItem() {
        return leftTicketItem;
    }

    public void setLeftTicketItem(LeftTicketItem leftTicketItem) {
        this.leftTicketItem = leftTicketItem;
    }

    public List<Passenger> getPassengers() {
        return passengers;
    }

    public void setPassengers(List<Passenger> passengers) {
        this.passengers = passengers;
    }

    public List<OrderTicketInfo> getTicketsInfo() {
        return ticketsInfo;
    }

    public void setTicketsInfo(List<OrderTicketInfo> ticketsInfo) {
        this.ticketsInfo = ticketsInfo;
    }

    public SeatType getSeatType() {
        return seatType;
    }

    public void setSeatType(SeatType seatType) {
        this.seatType = seatType;
    }
    
    public String getPurposeCodes() {
        return purposeCodes;
    }

    public void setPurposeCodes(String purposeCodes) {
        this.purposeCodes = purposeCodes;
    }

    public Date getTrainDate() {
        return trainDate;
    }

    public void setTrainDate(Date trainDate) {
        this.trainDate = trainDate;
    }
    
    /** 
     * 获取OldPassengerStr 
     *  
     * @param passengers 
     * @return 
     */  
    public String getOldPassengerStr() {
        StringBuilder oldPassengerStrBuilder = new StringBuilder();
        for (Passenger passenger : passengers) {
            oldPassengerStrBuilder.append(passenger.getPassengerName()).append(",")
                    .append(passenger.getPassengerIdTypeCode()).append(",")
                    .append(passenger.getPassengerIdNo()).append(",")
                    .append(passenger.getPassengerType())
                    .append("_");
        }
        return oldPassengerStrBuilder.toString();
    }  
    
    /**
     * 获取PassengerTicketStr
     * 
     * @return 
     */
    public String getPassengerTicketStr() {
        String bR = "";
        for (OrderTicketInfo ticketInfo : ticketsInfo) {
            String bT = "";
            if ("WZ".equals(ticketInfo.getSeatType())) {
                bT = OrderTicketInfo.getSeatType(OrderTicketInfo.getValidSeatMark(leftTicketItem.getQueryLeftNewDTO()));
            } else {
                bT = ticketInfo.getSeatType();
            }
            String bU = bT + ",0," + ticketInfo.getTicketType() + "," + ticketInfo.getPassengerName() + ","
                    + ticketInfo.getPassengerIdType() + "," + ticketInfo.getPassengerIdNo() + ","
                    + (ticketInfo.getPassengerPhoneNo() == null ? "" : ticketInfo.getPassengerPhoneNo()) + ",N";
            bR += bU + "_";
        }
        return bR.substring(0, bR.length() - 1);
    }
}
